<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" >
      <el-form-item label="类型名称" prop="saleTypeName">
        <el-input
          v-model="queryParams.saleTypeName"
          placeholder="请输入类型名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据类型名称" prop="billTypeName">
        <el-select v-model="queryParams.billTypeName" placeholder="请选择单据类型名称" clearable size="small">
          <el-option label="请选择字典生成" value=""/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['datafactory:saleTypeConfig:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['datafactory:saleTypeConfig:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['datafactory:saleTypeConfig:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['datafactory:saleTypeConfig:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border ref="fileTable" v-loading="loading" :data="saleTypeConfigList" @selection-change="handleSelectionChange" :row-class-name="tabRowClassName" @row-click="selectThisRow">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column v-if="false" label="单据类型名称" align="center" prop="id"/>
      <el-table-column label="类型编码" align="center" prop="saleTypeNo"/>
      <el-table-column label="类型名称" align="center" prop="saleTypeName"/>
      <el-table-column label="单据类型名称" align="center" prop="billTypeName"/>
      <el-table-column v-if="false" label="单据类型编码" align="center" prop="billTypeNo"/>
      <el-table-column label="集成表名称" align="center" prop="tableName" :formatter="tableNameFormat"/>
      <el-table-column label="业务类型编码" align="center" prop="bizTypeNo" :formatter="workTypeFormat"/>
      <el-table-column label="是否预定" align="center" prop="isBook" :formatter="bookTypeFormat"/>
      <el-table-column label="换货类型" align="center" prop="barterType" :formatter="barterTypeFormat"/>
      <el-table-column label="补差方式" align="center" prop="compensationMode" :formatter="compensationModeFormat"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['datafactory:saleTypeConfig:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['datafactory:saleTypeConfig:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改销售类型配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="类型名称" prop="saleTypeName">
              <el-input v-model="form.saleTypeName" placeholder="请输入类型名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="类型编码" prop="saleTypeNo">
              <el-input v-model="form.saleTypeNo" placeholder="请输入类型编码"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="单据类型名称" prop="billTypeNo">
              <el-select v-model="form.billTypeNo" filterable clearable placeholder="请选择单据类型名称"
                         @change="setBillName(form.billTypeNo)">
                <el-option
                  v-for="bill in billTypeOptions"
                  :key="bill.billTypeCode"
                  :label="bill.billTypeName"
                  :value="bill.billTypeCode"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="集成表名称" prop="tableName">
              <el-select v-model="form.tableName" filterable clearable placeholder="请输入业务表">
                <el-option
                  v-for="table in tableNameOptions"
                  :key="table.busTableName"
                  :label="table.busTableComment"
                  :value="table.busTableName"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="业务类型" prop="bizTypeNo">
              <el-select v-model="form.bizTypeNo" clearable placeholder="请输入业务类型">
                <el-option
                  v-for="dict in bizTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="是否预定" prop="isBook">
              <el-select clearable v-model="form.isBook">
                <el-option
                  v-for="dict in isBookOptions"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="换货类型" prop="barterType">
              <el-select v-model="form.barterType">
                <el-option
                  v-for="dict in barterTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="补差方式" prop="compensationMode">
              <el-select v-model="form.compensationMode">
                <el-option
                  v-for="dict in compensationModeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" :autosize="{ minRows:2,maxRows:4}" v-model="form.remark" placeholder="请输入备注"
                    style="width: 80%"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listSaleTypeConfig,
    getSaleTypeConfig,
    delSaleTypeConfig,
    addSaleTypeConfig,
    updateSaleTypeConfig
  } from "@/api/datafactory/saleTypeConfig";
  import {getTables} from "@/api/datafactory/table";
  import {getBillTypes} from "@/api/datafactory/billType";

  export default {
    name: "SaleTypeConfig",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 销售类型配置表格数据
        saleTypeConfigList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 业务表名称
        tableNameOptions: [],
        // 单据类型名称
        billTypeOptions: [],
        // 业务类型字典
        bizTypeOptions: [],
        // 是否预定
        isBookOptions: [{
          value: "true",
          label: "是"
        }, {
          value: "false",
          label: "否"
        }],
        // 换货类型字典
        barterTypeOptions: [],
        // 补差方式字典
        compensationModeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          saleTypeName: null,
          billTypeName: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          saleTypeNo: [
            {required: true, message: "类型编码不能为空", trigger: "blur"}
          ],
          saleTypeName: [
            {required: true, message: "类型名称不能为空", trigger: "blur"}
          ],
          billTypeName: [
            {required: true, message: "单据类型名称不能为空", trigger: "change"}
          ],
          billTypeNo: [
            {required: true, message: "单据类型编码不能为空", trigger: "blur"}
          ],
          tableName: [
            {required: true, message: "集成表名称不能为空", trigger: "change"}
          ],
          bizTypeNo: [
            {required: true, message: "业务类型编码不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();
      var table = {"tableStatus": 1};
      getTables(table).then(response => {
        this.tableNameOptions = response;
      });
      var status = {"status": 1}
      getBillTypes(status).then(response => {
        this.billTypeOptions = response;
      })
      this.getDicts("biz_type").then(response => {
        this.bizTypeOptions = response.data;
      });
      this.getDicts("barter_type").then(response => {
        this.barterTypeOptions = response.data;
      });
      this.getDicts("compensation_mode").then(response => {
        this.compensationModeOptions = response.data;
      });
    },
    methods: {
      /** 更改斑马纹 */
      tabRowClassName({row,rowIndex}){
        let index = rowIndex + 1;
        if(index %2 == 0){
          return 'two-color'
        }else {
          return "one-color"
        }
      },
      selectThisRow(row){
        this.$refs.fileTable.toggleRowSelection(row);
      },
      /** 查询销售类型配置列表 */
      getList() {
        this.loading = true;
        listSaleTypeConfig(this.queryParams).then(response => {
          this.saleTypeConfigList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          saleTypeNo: null,
          saleTypeName: null,
          billTypeName: null,
          billTypeNo: null,
          tableName: null,
          bizTypeNo: null,
          isBook: null,
          barterType: null,
          compensationMode: null,
          remark: null
        };
        this.resetForm("form");
      },
      // 业务类型字典翻译
      workTypeFormat(row, column) {
        return this.selectDictLabel(this.bizTypeOptions, row.bizTypeNo);
      },
      barterTypeFormat(row, column) {
        return this.selectDictLabel(this.barterTypeOptions, row.barterType);
      },
      compensationModeFormat(row, column) {
        return this.selectDictLabel(this.compensationModeOptions, row.compensationMode);
      },
      // 是否预定翻译
      bookTypeFormat(row, column) {
        if (row.isBook == "true") {
          return "是"
        } else if (row.isBook == "false") {
          return "否"
        }
      },
      setBillName(code) {
        this.billTypeOptions.forEach(item => {
          if (item.billTypeCode == code) {
            this.form.billTypeName = item.billTypeName;
          }
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加销售类型配置";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getSaleTypeConfig(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改销售类型配置";
        });
      },
      tableNameFormat(row, column) {
        var tableComment = "";
        this.tableNameOptions.forEach(item => {
          if (item.busTableName == row.tableName) {
            tableComment = item.busTableComment;
          }
        });
        return tableComment
      },
      billTypeFormat(row, column) {
        var billTypeNO = "";
        this.billTypeOptions.forEach(item => {
          if (item.billTypeName == row.billTypeName) {
            billTypeNO = item.billTypeNo;
          }
        });
        return billTypeNO
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateSaleTypeConfig(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSaleTypeConfig(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除销售类型配置编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delSaleTypeConfig(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('datafactory/saleTypeConfig/export', {
          ...this.queryParams
        }, `datafactory_saleTypeConfig.xlsx`)
      }
    }
  };
</script>
